//
//  WCBPayQRcodeCell.m
//  WeChatBuniess
//
//  Created by Guo Rui on 2017/12/19.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "WCBPayQRcodeCell.h"
#import "UIImageView+WCAWebCache.h"

@interface WCBPayQRcodeCell()
@property (strong, nonatomic) UIImageView *alipayQRCode;
@property (strong, nonatomic) UILabel *alipayDescLabel;
@property (strong, nonatomic) UIImageView *wechatQRCode;
@property (strong, nonatomic) UILabel *wechatDescLabel;
@property (strong, nonatomic) UILabel *totalPriceLabel;
@end

@implementation WCBPayQRcodeCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)setFrame:(CGRect)frame
{
    frame.origin.y += 20;
    frame.size.height -= 20;
    [super setFrame:frame];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat x, y, w, h;
    CGFloat width = CGRectGetWidth(self.contentView.frame);
    
    {
        x = 20.f;
        y = 20.f;
        w = width / 2 - 1.5 * 20.f;
        h = w;
        self.alipayQRCode.frame = CGRectMake(x, y, w, h);
    }
    
    {
        x = CGRectGetMinX(self.alipayQRCode.frame);
        y = CGRectGetMaxY(self.alipayQRCode.frame) + 15.f;
        w = CGRectGetWidth(self.alipayQRCode.frame);
        h = 40.f;
        self.alipayDescLabel.frame = CGRectMake(x, y, w, h);
    }
    
    {
        x = width / 2.f + 10.f;
        y = 20.f;
        w = width / 2 - 1.5 * 20.f;
        h = w;
        self.wechatQRCode.frame = CGRectMake(x, y, w, h);
    }
    
    {
        x = CGRectGetMinX(self.wechatQRCode.frame);
        y = CGRectGetMaxY(self.wechatQRCode.frame) + 15.f;
        w = CGRectGetWidth(self.wechatQRCode.frame);
        h = 40.f;
        self.wechatDescLabel.frame = CGRectMake(x, y, w, h);
    }
    
    {
        x = 30;
        y = CGRectGetMaxY(self.wechatDescLabel.frame) + 15.f;
        w = width - 2 * x;
        h = 40.f;
        self.totalPriceLabel.frame = CGRectMake(x, y, w, h);
    }
}

#pragma mark - Public
- (void)setPayModel:(WCBPayModel *)payModel
{
    _payModel = payModel;
    
    [self.alipayQRCode setImageURL:payModel.alipay_qrcode placeholder:nil];
    [self.wechatQRCode setImageURL:payModel.wechat_qrcode placeholder:nil];
    self.totalPriceLabel.text = [NSString stringWithFormat:@"¥%.2f(含运费)", payModel.totalPrice.floatValue];
}

#pragma mark - Getter
- (UIImageView *)alipayQRCode
{
    if (!_alipayQRCode) {
        _alipayQRCode = [[UIImageView alloc] init];
        _alipayQRCode.contentMode = UIViewContentModeScaleToFill;
        [self.contentView addSubview:_alipayQRCode];
    }
    return _alipayQRCode;
}

- (UIImageView *)wechatQRCode
{
    if (!_wechatQRCode) {
        _wechatQRCode = [[UIImageView alloc] init];
        _wechatQRCode.contentMode = UIViewContentModeScaleToFill;
        [self.contentView addSubview:_wechatQRCode];
    }
    return _wechatQRCode;
}

- (UILabel *)alipayDescLabel
{
    if (!_alipayDescLabel) {
        _alipayDescLabel = [[UILabel alloc] init];
        _alipayDescLabel.textColor = [UIColor blackColor];
        _alipayDescLabel.font = [UIFont systemFontOfSize:23];
        _alipayDescLabel.text = @"用支付宝扫一扫，完成支付";
        _alipayDescLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_alipayDescLabel];
    }
    return _alipayDescLabel;
}

- (UILabel *)wechatDescLabel
{
    if (!_wechatDescLabel) {
        _wechatDescLabel = [[UILabel alloc] init];
        _wechatDescLabel.textColor = [UIColor blackColor];
        _wechatDescLabel.font = [UIFont systemFontOfSize:23];
        _wechatDescLabel.text = @"用微信扫一扫，完成支付";
        _wechatDescLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_wechatDescLabel];
    }
    return _wechatDescLabel;
}

- (UILabel *)totalPriceLabel
{
    if (!_totalPriceLabel) {
        _totalPriceLabel = [[UILabel alloc] init];
        _totalPriceLabel.textColor = [UIColor redColor];
        _totalPriceLabel.font = [UIFont systemFontOfSize:28];
        _totalPriceLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_totalPriceLabel];
    }
    return _totalPriceLabel;
}
@end
